﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ProductManagement;

public partial class Modules_Production_ucProductSupplier : System.Web.UI.UserControl
{
    Products _products = new Products();
    void SearchSupplier()
    {
        try
        {
            string toSearch = string.Empty;
            gvwVendors.Visible = true;
            int count = (int)Session["Product_selectedProducts"];
            string productID = string.Empty;
            if (Session["Product_FromControl"].ToString() == "Production/ucAddProduct.ascx")
                toSearch = "new"; // show newly added Suppliers on top
            DataSet dsSupplier;
            _products.SearchVendor(productID, toSearch, out dsSupplier);
            gvwVendors.DataSource = dsSupplier;
            gvwVendors.Columns[1].Visible = true;

            gvwVendors.DataBind();

            gvwVendors.Columns[1].Visible = false;

        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "Search Suppliers Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }

    }

    void GetIDs()
    {
        try
        {
            int countSupplier = 0;
            string[] supplierID = new string[gvwVendors.Rows.Count];

            for (int i = 0; i < gvwVendors.Rows.Count; i++)  //checking selected Accessories
            {
                supplierID[i] = string.Empty;
                CheckBox chkGrid = (CheckBox)(gvwVendors.Rows[i].Cells[0].FindControl("chkEditDelete"));
                if (chkGrid.Checked == true)
                {
                    supplierID[countSupplier] = gvwVendors.Rows[i].Cells[1].Text;
                    countSupplier++;
                }
            }

            Session["Product_AddSupplierIDs"] = supplierID;
            Session["Product_SelectedSuppliers"] = countSupplier;
        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "Get IDs Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }
    }

    void AddSupplier()
    {
        try
        {
            int countSupplier = (int)Session["Product_SelectedSuppliers"];// no: of accessories selected(checked in the grid)           
            if (Session["Product_Add/EditProduct"] == "Added")  //if accessory is to be added to newly entered product
                Session["Product_selectedProducts"] = 1;
            int countProducts = (int)Session["Product_selectedProducts"];//no: of products selected

            string[] productIDs = (string[])Session["Product_IDs"];
            string[] supplerIDs = (string[])Session["Product_AddSupplierIDs"];
            string priority="0";

            for (int i = 0; i < countProducts; i++)
                for (int j = 0; j < countSupplier; j++)
                    _products.AddProductSupplier(productIDs[i], supplerIDs[j],priority);

        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "Add Product - Supplier Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }

    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string[] ID = new string[1];
            if (Session["Product_Add/EditProduct"].ToString() == "Added") //if new product is saved
            {
                ID[0] = _products.GetMaxProductID();
                Session["Product_IDs"] = ID;
            }
            SearchSupplier();
        }
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Session["UserManager_Control"] = "Production/ucAddProduct.ascx";
        Session["Product_FromControl"] = "Production/ucProductSupplier.ascx";
        Response.Redirect("BusinessProcessAutomation.aspx");
    }
    protected void btnSaveAccessory_Click(object sender, EventArgs e)
    {
        GetIDs();
        AddSupplier();

        if ((int)Session["Product_selectedProducts"] == 1) //if accessory is added to only 1 product, show those accessories
        {
            SearchSupplier();
        }

        Response.Write("<script>alert('Accessories Added Successfully !')</script>");
    }

    
    protected void btnAddAccessory_Click(object sender, EventArgs e)
    {
        Session["Product_Add/EditVendor"] = "Add";
        Session["Product_FromControl"] = "Production/ucProductSupplier.ascx";
        Session["UserManager_Control"] = "Production/ucAddVendor.ascx";
        Response.Redirect("BusinessProcessAutomation.aspx");
    }
}
